<template>
    <div>
        <tree ref="tree" :data="treeData" show-checkbox :render="renderContent"></tree>
    </div>
</template>

<script>
    export default {
        name: "menu-tree",
        props: {
            data: {
                type: Array,
                default() {
                    return []
                }
            },
        },
        watch: {
            data(newData) {
                this.treeData = this.mapToDir(newData)
            }
        },
        data() {
            return {
                treeData: [],
                buttonProps: {
                    type: 'default',
                    size: 'small',
                }
            }
        },
        methods: {
            getSelect() {
                return this.$refs.tree.getCheckedAndIndeterminateNodes()
            },
            mapToDir(data) {
                if (!data) return;
                return data.map(it => {
                    if (it.children) {
                        this.mapToDir(it.children)
                    }
                    let render = {
                        expand: true,
                        checked: it.checked,
                        render: (h, {root, node, data}) => {
                            return h('span', {
                                style: {
                                    display: 'inline-block',
                                    width: '100%'
                                }
                            }, [
                                h('span', [
                                    h('Icon', {
                                        props: {
                                            type: 'ios-folder-outline'
                                        },
                                        style: {
                                            marginRight: '8px'
                                        }
                                    }),
                                    h('span', data.title)
                                ]),
                                h('span', {
                                    style: {
                                        display: 'inline-block',
                                        float: 'right',
                                        marginRight: '32px'
                                    }
                                }, [])
                            ]);
                        }
                    }
                    return Object.assign(it, render)
                })

            },
            renderContent(h, {root, node, data}) {
                return h('span', {
                    style: {
                        display: 'inline-block',
                        width: '100%'
                    }
                }, [
                    h('span', [
                        h('Icon', {
                            props: {
                                type: 'ios-paper-outline'
                            },
                            style: {
                                marginRight: '8px'
                            }
                        }),
                        h('span', data.title)
                    ]),
                    h('span', {
                        style: {
                            display: 'inline-block',
                            float: 'right',
                            marginRight: '32px'
                        }
                    }, [])
                ]);
            },
            append(data) {
                const children = data.children || [];
                children.push({
                    title: 'appended node',
                    expand: true
                });
                this.$set(data, 'children', children);
            },
            remove(root, node, data) {
                const parentKey = root.find(el => el === node).parent;
                const parent = root.find(el => el.nodeKey === parentKey).node;
                const index = parent.children.indexOf(data);
                parent.children.splice(index, 1);
            }
        }
    }
</script>

<style scoped>

</style>
